package com.hmz.backend.mapper;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hmz.backend.entity.Message;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface MessageMapper  extends BaseMapper<Message> {
    @Insert("insert into message#{chatRoomId} values ${s}")
    boolean insertList(@Param("chatRoomId") int chatRoomId,@Param("s") String s);
    //批量插入消息
    @Insert("insert into message#{chatRoomId} values ${s}")
    void insertMessageList(@Param("chatRoomId") int chatRoomId,@Param("s") String s);

    @Select("select user_name as userName,content ,date_format(create_time,'%Y-%m-%d %H:%i') as createTime,type  from message#{chatRoomId},user where message#{chatRoomId}.receiver_id = #{chatRoomId} and message#{chatRoomId}.sender_id = #{userId} and sender_id = user_id ")
    List<JSONObject> selectShowList( @Param("chatRoomId") int chatRoomId, @Param("userId") int userId);
    /**
     * 返回指定聊天室所有的日期（天）列表
     */
    @Select("select date_format(create_time,'%Y-%m-%d') as value1,date_format(create_time,'%Y-%m-%d') as value2 from message#{chatRoomId} where message#{chatRoomId}.receiver_id = #{chatRoomId}  group by date_format(create_time,'%Y-%m-%d') ")
    List<JSONObject> getTimeListInChatRoom(int chatRoomId);
    /**
     * 返回指定聊天室下指定类型的消息记录
     */
    @Select("select user_name as userName,content ,date_format(create_time,'%Y-%m-%d %H:%i') as createTime,type  from message#{chatRoomId},user where message#{chatRoomId}.receiver_id = #{chatRoomId} and message#{chatRoomId}.type = #{type} and sender_id = user_id ")
    List<JSONObject>  getTypeMessageListInChatRoom(@Param("chatRoomId") int chatRoomId,@Param("type") int type);
    /**
     * 返回指定聊天室下指定日期的消息记录
     */
    @Select("select user_name as userName,content ,date_format(create_time,'%Y-%m-%d %H:%i') as createTime,type from message#{chatRoomId} ,user where message#{chatRoomId}.receiver_id = #{chatRoomId} and date_format(create_time,'%Y-%m-%d') = #{date} and sender_id = user_id")
    List<JSONObject> getTimeMessageListInChatRoom(@Param("chatRoomId") int chatRoomId, @Param("date")String date);

    //分页查询返回最大页数
    @Select("select CEILING(count(id)/#{pageSize})  pageCount from ${tableName}")
    int getMaxPages(@Param("tableName") String tableName, @Param("pageSize") int pageSize);
    //获取更早的消息记录
    @Select("select id,sender_id,receiver_id,content,create_time from ${tableName}  order by id desc limit ${(pageNow-1) * pageSize } , ${pageSize}  ")
    List<Message> getOldMessage(@Param("tableName") String tableName , @Param("pageNow") int pageNow, @Param("pageSize") int pageSize);

}
